<template>
  <div class="add-form">
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="30%" @close="() => formBase = {isCommon: 1, parent: {name: ''}}">
      <el-form ref="dataForm" :model="formBase" label-position="left" label-width="150px">
        <el-form-item label="服务分类：" prop="userName">{{formBase.parent.name}}</el-form-item>
        <el-form-item label="子分类编号：" prop="name">
          <el-input v-model="formBase.code"  placeholder="请输入子分类编号"></el-input>
        </el-form-item>
        <el-form-item label="子分类名称：" prop="pwd">
          <el-input v-model="formBase.name"  placeholder="请输入子分类名称"></el-input>
        </el-form-item>
        <el-form-item label="是否通用：" prop="pwdOk">
          <el-select v-model="formBase.isCommon" placeholder="请选择">
            <el-option label="通用" :value="1"></el-option>
            <el-option label="不通用" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">取消</el-button>
        <el-button type="primary" @click="createData">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import baseDate from '@/api/base/baseApi'
import { updateSubData, addSubData, getSubDetails } from '@/api/serviceType'
export default {
  name: 'add',
  props: ['dialogTitle'],
  data() {
    return {
      dialogFormVisible: false,
      typeId: '',
      formBase: {isCommon: 1, parent: {name: ''}},
      baseDate: {},
      parentData: {}
    }
  },
  computed: {},
  methods: {
    // 弹层显示
    async dialogFormV(val) {
      this.parentData = {}
      this.formBase.parent = val
      this.formBase.parentId = val.id
      if (val.change !== undefined) {
        this.formBase = val

      }
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH() {
      this.dialogFormVisible = false
    },

    // 表单提交
    createData() {
      this.$refs['dataForm'].validate(async valid => {
        if (valid) {
          this.dialogFormH()
          if (this.formBase.id) {
            await updateSubData(this.formBase).then(() => {
              this.$emit('newDataes', this.formBase)
            })
          } else {
            await addSubData(this.formBase).then(() => {
              this.$emit('newDataes', this.formBase)
            })
          }
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    }
  },
  // 挂载结束

  mounted: function() {},
  // 创建完毕状态
  created() {
    this.baseDate = baseDate
  },
  // 组件更新
  updated: function() {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
